<template>
  <div>
    <a-form ref="entityForm" :model="entity" :rules="rules">
      <a-form-item hide-label field="changdu">
        <a-input v-model="entity.changdu" ref="changdu">
          <template #append>长度(mm)</template>
        </a-input>
      </a-form-item>
      <a-form-item hide-label field="zhijing">
        <a-input v-model="entity.zhijing"  >
          <template #append>直径(mm)</template>
        </a-input>
      </a-form-item>
      <a-form-item hide-label field="caizhi">
        <a-select v-model="entity.caizhi" placeholder="选择材质" allow-clear allow-search @change="caizhiSelect">
          <a-option v-for="item in state.caiZhiList" :value="item.id" :label="item.name" :key="'caizhi' + item.id" />
        </a-select>
      </a-form-item>
      <a-form-item hide-label field="jiage">
        <a-input v-model="entity.jiage">
          <template #append>材质吨单价</template>
        </a-input>
      </a-form-item>
      <a-form-item hide-label field="duanzao">
        <a-select v-model="entity.duanzao" placeholder="选择钢类" allow-clear allow-search @change="duanzaoSelect">
          <a-option v-for="item in state.duanZaoList" :value="item.id" :label="item.name" :key="'duanzao' + item.id" />
        </a-select>
      </a-form-item>
      <a-form-item hide-label field="gongyi">
        <a-select v-model="entity.gongyi" placeholder="选择加热工艺" allow-clear  allow-search @change="jiaReGongyiSelect">
          <a-option v-for="item in state.jiaregongyiList" :value="item.id" :label="item.name" :key="'jrgongyi' + item.id" />
        </a-select>
      </a-form-item>
      <a-form-item hide-label field="dzgongyi">
        <a-select v-model="entity.dzgongyi" placeholder="选择锻造工艺" allow-clear  allow-search @change="dzGongyiSelect">
          <a-option v-for="item in state.dzgongyiList" :value="item.id" :label="item.name" :key="'zdgongyi' + item.id" />
        </a-select>
      </a-form-item>
      <a-form-item hide-label field="hlgongyi">
        <a-select v-model="entity.hlgongyi" placeholder="选择缓冷工艺" allow-clear  allow-search @change="hlGongyiSelect">
          <a-option v-for="item in state.hlgongyiList" :value="item.id" :label="item.name" :key="'hlongyi' + item.id" />
        </a-select>
      </a-form-item>
      <a-form-item hide-label field="jjgongyi">
        <a-select v-model="entity.jjgongyi" placeholder="选择机加工艺" allow-clear  allow-search @change="jjGongyiSelect">
          <a-option v-for="item in state.jjgongyiList" :value="item.id" :label="item.name" :key="'jjgongyi' + item.id" />
        </a-select>
      </a-form-item>
      <a-form-item hide-label field="chengben" v-if="false">
        <a-input v-model="entity.chengben">
          <template #append>工艺吨成本</template>
        </a-input>
      </a-form-item>
      <a-form-item hide-label field="liaobi">
        <a-input v-model="entity.liaobi" >
          <template #append>料比</template>
        </a-input>
      </a-form-item>
      <a-form-item hide-label field="feiyong">
        <a-input v-model="entity.feiyong" readonly>
          <template #append>预计吨成本</template>
        </a-input>
      </a-form-item>
    </a-form>
    <div style="text-align: center">
      <a-button type="primary" @click="btnCalc">计算</a-button>
    </div>
  </div>
</template>

<script lang="ts" setup>
import axios from "axios";
import { Message } from '@arco-design/web-vue'
import mittBus from "@/utils/route-listener"
import {reactive, ref, onMounted, defineProps, defineEmits, nextTick} from "vue";
import GongYiType from "@/utils/gongYiType";

const entityForm = ref(null)
const state = reactive({
  caiZhiList : [],
  duanZaoList : [],

  jiaregongyiList : [],
  dzgongyiList : [],
  hlgongyiList : [],
  jjgongyiList : [],
})
let entity = ref({
  changdu:undefined,
  zhijing:undefined,
  caizhi:undefined,
  jiage:'',   //材质吨价格
  duanzao:undefined,  //钢类锻造基础
  duanzaofei:'',  //钢类锻造基础费

  gongyi:undefined,//加热工艺
  dzgongyi:undefined,//锻造工艺
  hlgongyi:undefined,//缓冷工艺
  jjgongyi:undefined,//机加工工艺
  jiaReCb:'', //加热工艺成本
  duanZaoCb:'',//锻造工艺成本
  huanLengCb:'',//缓冷工艺成本
  jiJiaCb:'',//机加工工艺成本
  chengben:'',  //总体的工艺成本

  liaobi: '',
  feiyong: '',  //计算后的吨成本
})
let xishuEntity = ref({
  changdu: '',
  zhijing: '',
  calctype: '',
  xishu: '',
})
const rules = reactive({
  changdu: [
    { required: true, message: '必填项', trigger: 'blur' },
    { trigger: 'blur', type: 'number', min: 0,message: '大于0的数字', }
  ],
  zhijing: [
    { required: true, message: '必填项', trigger: 'blur' },
    { trigger: 'blur', type: 'number', min: 0,message: '大于0的数字', }
  ],
  liaobi: [
    { required: true, message: '请输入料比', trigger: 'blur' },
    { trigger: 'blur', type: 'number', min: 0,message: '大于0的数字', }
  ],
  jiage: [
    { required: true, message: '请输入材质吨单价', trigger: 'blur' },
    { trigger: 'blur', type: 'number', min: 0,message: '大于0的数字', }
  ],
  caizhi: [
    { required: true, message: '请选择材质', trigger: 'blur' },
  ],
  duanzao: [
    { required: true, message: '请选择钢类', trigger: 'blur' },
  ],
  gongyi: [
    { required: true, message: '必填项', trigger: 'blur' },
  ],
  dzgongyi: [
    { required: true, message: '必填项', trigger: 'blur' },
  ],
  hlgongyi: [
    { required: true, message: '必填项', trigger: 'blur' },
  ],
  jjgongyi: [
    { required: true, message: '必填项', trigger: 'blur' },
  ]
})
const caizhiSelect = (value)=>{
  const caizhiObj =  state.caiZhiList.filter(it => it.id === value)[0]
  if(caizhiObj){
    entity.value.jiage = caizhiObj.jiage
    loadGongYiByCaiZhi(value);
  }else {
    entity.value.jiage = ''
  }
  entity.value.gongyi = undefined
  entity.value.dzgongyi = undefined
  entity.value.hlgongyi = undefined
  entity.value.jjgongyi = undefined
  entity.value.jiaReCb = ''
  entity.value.duanZaoCb =  ''
  entity.value.huanLengCb =  ''
  entity.value.jiJiaCb =  ''
  entity.value.chengben = ''
  entity.value.feiyong = ''
}
const duanzaoSelect = (value)=>{
  const duanzaoObj =  state.duanZaoList.filter(it => it.id === value)[0]
  if(duanzaoObj){
    entity.value.duanzaofei = duanzaoObj.chengben
  }else {
    entity.value.duanzaofei = ''
  }
  entity.value.feiyong = ''
}
const loadGongYiByCaiZhi = (caizhiid)=>{
  axios.post('/gongyi/listcaizhigongyi', {id : caizhiid, gongyitype : GongYiType.duanzao}).then(res => {
    const { data } = res
    state.dzgongyiList = data
  })
  axios.post('/gongyi/listcaizhigongyi', {id : caizhiid, gongyitype : GongYiType.huanleng}).then(res => {
    const { data } = res
    state.hlgongyiList = data
  })
  axios.post('/gongyi/listcaizhigongyi', {id : caizhiid, gongyitype : GongYiType.jijia}).then(res => {
    const { data } = res
    state.jjgongyiList = data
  })
}
const jiaReGongyiSelect = (value)=>{
  const gongyiObj =  state.jiaregongyiList.filter(it => it.id === value)[0]
  if(gongyiObj){
    entity.value.jiaReCb = gongyiObj.chengben
  }else {
    entity.value.jiaReCb = ''
  }
  entity.value.feiyong = ''
  entity.value.chengben = ''
}
const dzGongyiSelect = (value)=>{
  const gongyiObj =  state.dzgongyiList.filter(it => it.id === value)[0]
  if(gongyiObj){
    entity.value.duanZaoCb = gongyiObj.chengben
  }else {
    entity.value.duanZaoCb = ''
  }
  entity.value.feiyong = ''
  entity.value.chengben = ''
}
const hlGongyiSelect = (value)=>{
  const gongyiObj =  state.hlgongyiList.filter(it => it.id === value)[0]
  if(gongyiObj){
    entity.value.huanLengCb = gongyiObj.chengben
  }else {
    entity.value.huanLengCb = ''
  }
  entity.value.feiyong = ''
  entity.value.chengben = ''
}
const jjGongyiSelect = (value)=>{
  const gongyiObj =  state.jjgongyiList.filter(it => it.id === value)[0]
  if(gongyiObj){
    entity.value.jiJiaCb = gongyiObj.chengben
  }else {
    entity.value.jiJiaCb = ''
  }
  entity.value.feiyong = ''
  entity.value.chengben = ''
}
const btnCalc = ()=>{
  if(!entityForm.value) return;
  entityForm.value.validate().then((errors) =>{
    if(!errors){
      const jiage = Number(entity.value.jiage)
      const duanzaofei = Number(entity.value.duanzaofei)

      const jiaReCb = Number(entity.value.jiaReCb)
      const duanZaoCb = Number(entity.value.duanZaoCb)
      const huanLengCb = Number(entity.value.huanLengCb)
      const jiJiaCb = Number(entity.value.jiJiaCb)
      let chengben = jiaReCb+duanZaoCb+huanLengCb+jiJiaCb

      const maxChangDu = Number(xishuEntity.value.changdu)
      const maxZhiJing = Number(xishuEntity.value.zhijing)
      const xishu = Number(xishuEntity.value.xishu)
      if(!(isNaN(maxChangDu) || isNaN(maxZhiJing ) || isNaN(xishu))){
        if(entity.value.changdu >= maxChangDu || entity.value.zhijing >= maxZhiJing){
          if(xishuEntity.value.calctype === '乘法'){
            chengben = chengben*xishu
          }else {
            chengben = chengben+xishu
          }
        }
      }

      const liaobi = Number(entity.value.liaobi)
      const feiyong = liaobi * (jiage + duanzaofei + chengben)
      entity.value.feiyong = feiyong.toFixed(1)
    }else{
      console.log('error submit!!');
      return false;
    }
  })
}

const loadCaiZhi = ()=>{
  axios.post('/caizhi/listallcaizhi', {}).then(res => {
    const { data } = res
    state.caiZhiList = data
  })
}
const loadDuanZao = ()=>{
  axios.post('/duanzao/listallduanzao', {}).then(res => {
    const { data } = res
    state.duanZaoList = data
  })
}
const loadJiaReGongYi = ()=>{
  axios.post('/gongyi/listjiaregongyi', {}).then(res => {
    const { data } = res
    state.jiaregongyiList = data
  })
}
const loadXiShu = ()=>{
  axios.post( '/myconfig/listconfig',{} ).then( res =>{
    const configList = res.data
    xishuEntity.value.changdu = configList.find(x => x.itemkey==='changdu')?.itemvalue || ""
    xishuEntity.value.zhijing = configList.find(x => x.itemkey==='zhijing')?.itemvalue || ""
    xishuEntity.value.calctype = configList.find(x => x.itemkey==='calctype')?.itemvalue || ""
    xishuEntity.value.xishu = configList.find(x => x.itemkey==='xishu')?.itemvalue || ""
  })
}
onMounted(()=>{
  loadCaiZhi();
  loadDuanZao();
  loadJiaReGongYi();
  loadXiShu();
  nextTick(()=>{})
})
</script>

<style scoped>

</style>
